<template>
  <div class="dialog">
    <el-dialog :title="'作业记录' + titleText" :visible.sync="showDialog" width="45%" top="0" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" label-position="left" :model="formData" :rules="rules" class="el-form">
        <el-form-item label="鱼塘名称:" prop="fishPondId" class="w50p">
          <el-select filterable clearable v-model="formData.fishPondId" @change="queryBatch">
            <el-option v-for="(item, index) in pondList" :key="index" :label="item.pondName" :value="item.fishPondId" />
          </el-select>
        </el-form-item>
        <el-form-item label="批次编号:" prop="productionBatchId" class="w50p">
          <el-select filterable clearable v-model="formData.productionBatchId" >
            <el-option v-for="(item, index) in batchList" :key="index"
              :label="`${item.mnecode}_${item.varietyName}`" :value="item.productionBatchId" />
          </el-select>
        </el-form-item>
        <el-form-item label="位置：" class="w50p" prop="position">
          <el-input v-model="formData.position" clearable placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="季节：" class="w50p" prop="season">
          <el-select filterable clearable v-model="formData.season">
            <el-option label="春季" value="春季" />
            <el-option label="夏季" value="夏季" />
            <el-option label="秋季" value="秋季" />
            <el-option label="冬季" value="冬季" />
          </el-select>
        </el-form-item>
        <el-form-item label="作业人:" prop="staffName" class="w50p">
          <el-select filterable clearable v-model="formData.staffName"  @change="staffChange">
            <el-option v-for="(item, index) in staffList" :key="index" :label="item.staffName" :value="item.staffName" />
          </el-select>
        </el-form-item>
        <el-form-item label="作业时间：" class="w50p" prop="workBeginTime">
          <el-date-picker v-model="formData.workBeginTime" type="datetime" placeholder="请选择时间"
            value-format="yyyy-MM-dd HH:mm:ss" clearable />
        </el-form-item>
        <el-form-item label="作业天数：" class="w50p" prop="day">
          <el-input v-model="formData.day" type="number" clearable placeholder="请输入作业天数" />
        </el-form-item>
        <el-form-item label="作业次数：" class="w50p" prop="time">
          <el-input v-model="formData.time" type="number" clearable placeholder="请输入作业次数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import DewNetOperationApi from "@/api/operation-management/dew-net-operation"
import BatchMangeApi from "@/api/batch-mange"
import { dialogMixin } from "@/utils/dialogMixin";
export default {
  name: '',
  components: {},
  mixins: [dialogMixin],
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
    staffList: {
      type: [Array],
      default: () => {
        return []
      }
    },
    pondList: {
      type: [Array],
      default: () => {
        return []
      }
    },
    materialList: {
      type: [Array],
      default: () => {
        return []
      }
    },
  },
  data () {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      rules: {
        fishPondId: [
          { required: true, message: "请输入鱼塘名称", trigger: ["change", "blur"] }
        ],
        productionBatchId: [
          { required: true, message: "请输入批次编号", trigger: ["change", "blur"] }
        ],
        position: [
          { required: true, message: "请输入位置", trigger: ["change", "blur"] }
        ],
        season: [
          { required: true, message: "请选择季节", trigger: ["change", "blur"] }
        ],
        staffName: [
          { required: true, message: "请选择作业人", trigger: ["change", "blur"] }
        ],
        workBeginTime: [
          { required: true, message: "请选择作业时间", trigger: ["change", "blur"] }
        ],
        day: [
          { required: true, message: "请输入作业天数", trigger: ["change", "blur"] }
        ],
        time: [
          { required: true, message: "请输入作业次数", trigger: ["change", "blur"] }
        ],
      },
      baseurl: this.$baseurl.fileServiceUrl + 'base/files/upload',
      headers: {
        token: sessionStorage.getItem('token'),
        homeBaseId: sessionStorage.getItem('homeBaseId'),
        homeBaseType: sessionStorage.getItem('homeBaseType'),
      },
      images: [],
      fileIdList: [],
      formData: {},
      batchList: [],//批次
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show (v) {
      this.showDialog = v;
      if (!v) {
        this.$refs['ruleForm'].resetFields();
        this.isChooseWeightRecord = false;
        console.log('重置表单验证')
      }
    },
    nowChanceStau (v) {
      if (v === 'add') {
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
          staffId: sessionStorage.getItem('staffId'),
          workBeginTime: this.getNowTime()
        }
      }
      if (v === 'edit') {
        this.formData = this.editObject
        this.queryBatch(this.formData.fishPondId)
      }
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    queryBatch (e) {
      if (e) {
        BatchMangeApi.queryBatchByPondId({ pondId: e }).then(res => {
          this.batchList = res.data
        })
      } else {
        this.batchList = []
      }
    },
    staffChange (e) {
      this.staffList.find((item) => {
        if (item.staffId === e) {
          this.formData.staffName = item.staffName
          return true
        }
      })
    },
    save (formData) {
      this.formData.overdueFlag = false
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            DewNetOperationApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            DewNetOperationApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close () {
      this.$emit("closeDialog");
    },
    cancel () {
      this.$emit("cancelDialog");
    }
  }
};
</script>

<style lang="scss" scoped></style>
